// pages/my/index.js
import Toast from 'tdesign-miniprogram/toast/index';
// 接口
import { fetchGetUserinfo, fetchUserSignRecord, fetchUserSignin, fetchUserBindWxPhone } from '~/request/api';
const grids = [
  { label: '个人/监护人信息', value: 'personInfo', icon: 'star', page: '/pages/my/info/index?type=person' },
  { label: '孩子信息', value: 'childInfo', icon: 'star', page: '/pages/my/info/index?type=child' },
  { label: '借阅记录', value: 'borrowingRecords', icon: 'star', page: '/pages/my/borrowing-records/index' },
  { label: '收货地址', value: 'address', icon: 'star', page: '/pages/usercenter/address/list/index' },
  { label: '我的积分', value: 'integral', icon: 'star', page: '/pages/my/my-integral/index' },
  { label: '购买记录', value: 'purchaseRecords', icon: 'star', page: '/pages/my/purchase-records/index' },
  { label: '兑换记录', value: 'exchangeRecords', icon: 'star', page: '/pages/my/exchange-records/index' },
  { label: '话费管理', value: 'phoneBill', icon: 'star' },
];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:
      'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    currentStep: 0,
    steps: [],
    grids: grids,
    info: null,
  },

  onStepChange(e) {
    this.setData({ currentStep: e.detail.current });
  },

  handleGrid(e) {
    const { page } = e.currentTarget.dataset;
    wx.navigateTo({ url: page });
  },

  handleWeek() {
    this.userSignin();
  },

  // 手机授权
  getPhoneNumber(e) {
    const { code } = e.detail;
    this.userBindWxPhone(code);
  },

  // 授权用户设置头像
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    this.setData({
      avatarUrl,
    });
    this.onRefresh();
  },

  /**
   * 接口函数
   */

  // 绑定手机号
  async userBindWxPhone(code) {
    try {
      const res = await fetchUserBindWxPhone({ code });
      Toast({
        context: this,
        selector: '#t-toast',
        message: res.msg,
      });
      this.getUserinfo()
    } catch (error) {
      console.log(error);
    }
  },

  // 获取用户信息
  async getUserinfo() {
    try {
      const res = await fetchGetUserinfo();
      this.setData({ info: res.data });
    } catch (error) {
      console.log(error);
    }
  },

  // 获取签到记录
  async userSignRecord() {
    try {
      const res = await fetchUserSignRecord();
      this.setData({ steps: res });
    } catch (error) {
      console.log(error);
    }
  },

  // 签到
  async userSignin() {
    try {
      const res = await fetchUserSignin();
      Toast({
        context: this,
        selector: '#t-toast',
        message: res.msg,
      });
      this.userSignRecord();
    } catch (error) {
      console.log(error);
    }
  },

  init() {
    this.getUserinfo();
    this.userSignRecord();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.init();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
